<template>
	<view class="ECG">
		<view class="state">{{lianjie}}</view>
		<view class="data"><image :src="$IMAGE_BASE_URL + '/img/ECG.png'" style="width: 50rpx;height: 50rpx; float: left;margin-right: 10rpx;"></image>{{heartRate}}<text style="font-size: 22rpx;">(bmp)</text></view>
		<Draw ref="childRef" :text="''" :bg="'bg-aaa'" :line="'line-a'" @data-from-child="handleDataFromChild" />
		<!-- <view>
			<view style="font-size: 50rpx;margin: 20rpx 0;">历史记录</view>
			<view>2024年12月6日14:38:24</view>
			<Draw2 ref="childRef" :text="''" :bg="'bg-b'" :line="'line-b'" @data-from-child="handleDataFromChild" />
			<view>2024年12月6日14:38:24</view>
			<Draw2 ref="childRef" :text="''" :bg="'bg-c'" :line="'line-c'" @data-from-child="handleDataFromChild" />
			<view>2024年12月6日14:38:24</view>
			<Draw2 ref="childRef" :text="''" :bg="'bg-d'" :line="'line-d'" @data-from-child="handleDataFromChild" />
			<view>2024年12月6日14:38:24</view>
			<Draw2 ref="childRef" :text="''" :bg="'bg-e'" :line="'line-e'" @data-from-child="handleDataFromChild" />
		</view> -->
	</view>
</template>

<script>
	import {
		getCurrentPage
	} from '@/core/app'
	import CacheConfig from "../../config/CacheConfig";
	import SimDevice from '../../service/mqtt/sim/SimDevice.js';
	import {DeviceBasicData} from '../../service/mqtt/DeviceConfig.js';
	
	import {
		device
	} from "../../api/login/index.js";
	import {
		getPropert
	} from "../../api/device/index.js";
	import StorageUtils from "../../utils/StorageUtils";
	import drawVue from '../draw/draw.vue';
	import Draw from '@/pages/draw/draw.vue';
	import Draw2 from '@/pages/draw/draw2.vue';
	import WebSocketClient from '../../service/mqtt/WebSocketClient';
	import Config from '@/core/config'
	
	export default {
		components: {
			Draw,
			Draw2
		},
		data() {
			return {
				show: false,
				datas: [], //分析数据
				range: 1, //时间
				chart: false, //是否显示折线tu
				info: {},
				deviceList:[],
				productKey:'',
				lianjie: '导联未连接',
				heartRate:0
			}
		},
		onReady() {
			this.iotDeviceId = getCurrentPage().query.iotDeviceId;
			this.productKey = getCurrentPage().query.productKey;
		},
		onShow() {
			// this.mqtt(getCurrentPage().query.iotDeviceId,getCurrentPage().query.productKey)
			const apiUrl = Config.get('apiUrl3')
			this.deviceData = setInterval(() => {
				this.$u.get(apiUrl+'device/get/propert', {
								deviceId: getCurrentPage().query.iotDeviceId
							}).then(res => {
								this.heartRate=res.obj.heartRate.value
								// console.log(res.obj.connection.value);
								let lianjie=res.obj.connection.value=='0'?'导联未连接':'导联已连接'
								this.lianjie=lianjie
							});
			}, 1000);
		},
		onHide() {
			clearInterval(this.deviceData);
		},
		onUnload() {
			clearInterval(this.deviceData);
			// if (this.mqttPing) {
			// 	clearInterval(this.mqttPing);
			// 	this.simDevice.close();
			// }
		},
		methods: {
			mqtt() {
				let deviceBasicData = new DeviceBasicData('', '', undefined);
				this.simDevice = new SimDevice();
				let url = '47.97.72.174:1887';
				// let url = '123.60.177.231:1887';
				deviceBasicData.username = '1559108322@qq.com';
				deviceBasicData.password = '123456';
				deviceBasicData.addr = url;
				let that = this;
				this.simDevice.clientDataListener = {
					onHandle: (topic, msg) => {
						msg = msg.replace(/'/g, '"');
						if (JSON.parse(msg).method === 'thing.service.property.post') {
							let info = JSON.parse(msg);
							if (getCurrentPage().query.deviceName == info.deviceName) {
								this.device= {
									...this.device,
									...info.params
								}
							}
							console.log(this.device);
						}
					},
					onSetProperty: (requestPayload) => {
						let params = requestPayload.params;
						// let b = this.simDevice.resSetPropertyValue(responsePayload);
						// if (this.state.send){
						//   this.handleSubmit()
						// }
					},
					onConnect(success) {
						if (success) {
							that.simDevice.subscribe(getCurrentPage().query.productKey, getCurrentPage().query.deviceName)
							that.mqttPing = setInterval(() => {
								that.simDevice.ping()
							}, 30000);
						}
					},
					onClose() {
						console.log('连接关闭');
						// that.simDevice.connectServer();
					}
				};
				this.simDevice.init(deviceBasicData);
				// this.mqttLog = this.simDevice.mqttDevice.connect.mqttLog;
				this.simDevice.connectServer();
			},
			
		},

	};
</script>

<style lang="less">
	.ECG {
		padding: 15px;
		font-size: 14px;
		.state{
			width: 300rpx;
			position: fixed;
			line-height: 52rpx;
			font-size: 32rpx;
			text-align: center;
			border-radius: 26rpx;
			background-color: #1f88ff;
			top: 450rpx;
			color: #fff;
			left: 70rpx;
			z-index: 1995;
		}
		.data{
			position: fixed;
			line-height: 50rpx;
			top: 450rpx;
			right: 30rpx;
			z-index: 1995;
			font-size: 50rpx;
		}
	}
</style>